﻿package com.htmlparser;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.tags.FrameSetTag;
import org.htmlparser.tags.FrameTag;
import org.htmlparser.util.NodeList;

import com.pc.util.http.PCHttp;

public class Frameset {
	
	public static String doFrame(String html, String encoding, String path, String name) throws Exception{
		String htmltext = "";
		// 提取当前页面所有的frameset
		Parser FrameSet_parser = new Parser();
		FrameSet_parser.setInputHTML(html);
		FrameSet_parser.setEncoding(encoding);
		NodeFilter FrameSet_Filter = new NodeClassFilter(FrameSetTag.class);
		NodeList FrameSet_List = FrameSet_parser.extractAllNodesThatMatch(FrameSet_Filter);
		NodeList Frame_List = null;

		// 把解析每一个frame得到的报文拼成一个字符串，一次性传给手机端
		StringBuffer all_message = new StringBuffer();

		System.out.println("");
		System.out.println("===================   frame开始   =====================");
		System.out.println("");

		if (FrameSet_List != null) {
			System.out.println("~~~~~~~~~~~~~~~~ Go into FrameSet_List  ~~~~~~~~~~~~~~~");
			for (int i = 0; i < FrameSet_List.size(); i++) {
				Node FrameSetNode = FrameSet_List.elementAt(i);
				if ((FrameSetNode != null)
						&& (FrameSetNode instanceof FrameSetTag)) {
					// 获取当前frameset下的所有frame
					Frame_List = ((FrameSetTag) FrameSetNode).getChildren();

					// frame的src属性链接到的url
					String frame_location = "";

					// 解析后得到的报文
					String message = "";

					// 根据从frame的src得到的url，从原OA系统提取到网页内容
					String frame_inputHtml[] = new String[2];

					for (int j = 0; j < Frame_List.size(); j++) {
						Node FrameNode = Frame_List.elementAt(j);
						if ((FrameNode != null)
								&& (FrameNode instanceof FrameTag)) {

							// ===================================
							frame_location = ((FrameTag) FrameNode).getFrameLocation();
							System.out.println("=============== Frameset boss传的URL前缀:" + path);
							System.out.println("#################### 修改前：frame_location=="+ frame_location + "==");
							if((frame_location != null) && !(frame_location.equals(""))){
								//如果路径是../开头
								if(String.valueOf(frame_location.charAt(0)).equals(".")){
									frame_location = frame_location.substring(3, frame_location.length());
									
									String html_url[] = path.split("/");
									StringBuffer url = new StringBuffer();
									for (int k = 0; k < html_url.length-1; k++) {
										//System.out.println(html[k]);
										url.append(html_url[k] + "/");
									}
									frame_location = url.toString() + frame_location;
								}
								//如果路径前没有/
								else if(!(String.valueOf(frame_location.charAt(0)).equals("/"))){
									frame_location = path + frame_location;
								}
							}
							System.out.println("#################### 修改后：frame_location=="+ frame_location + "==");
							
							// ===================================
							name = "mashenghai";
							frame_inputHtml = PCHttp.sendGet(name, null,frame_location, "GBK");
							
							System.out.println("frame_inputHtml【"+i+"】==" + frame_inputHtml[1] + "==");
							
							message = MyParse62.getContent(frame_inputHtml[0], path,true);
							all_message.append(message);
						}
					}
				}
			}
			htmltext = all_message.toString();
		}
		
		//TODO 处理htmltext可能有多个<data></data>对的情况
		htmltext = htmltext.replaceAll("<data>", "").replaceAll("</data>", "").replaceAll("<data />", "");
		htmltext = "<data>" + htmltext + "</data>";
		
		System.out.println("");
		System.out.println("==================   拼接Frame的内容 begain   ====================");
		System.out.println(htmltext);
		System.out.println("==================   拼接Frame的内容 end   ====================");
		System.out.println("");
		System.out.println("");
		System.out.println("==================   frame结束   ====================");
		System.out.println("");
		
		return htmltext.replaceAll("&amp;nbsp", "");
	}

}
